package com.example.mqtt.service;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.example.mqtt.domain.RecordData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class RedisService {

    @Autowired
    RedisTemplate redisTemplate;

    private RuntimeSchema<RecordData> recordDataSchema = RuntimeSchema.createFrom(RecordData.class);



    public RecordData getRecordData(String mcuId) {
        try {
            // 并没有实现哪部序列化操作
            // 采用自定义序列化
            // protostuff: pojo.
            RecordData o = (RecordData) redisTemplate.opsForValue().get(mcuId);
//            byte[] bytes = o.getBytes();
//            // 缓存重获取到
//            if (bytes != null) {
//                RecordData recordData = recordDataSchema.newMessage();
//                ProtostuffIOUtil.mergeFrom(bytes, recordData, recordDataSchema);
//                return recordData;
//            }
            return o;

        } catch (Exception e) {
                e.printStackTrace();
        }
        return null;
    }

    public void putRecordData(String mcuId, RecordData recordData) {
        try {
//            byte[] bytes = ProtostuffIOUtil.toByteArray(recordData, recordDataSchema,
//                    LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//            // 超时缓存
            long timeout = 60 * 60 * 24l * 2;// 24 小时
//            redisTemplate.opsForValue().set(mcuId,new String(bytes),timeout,TimeUnit.SECONDS);

            redisTemplate.opsForValue().set(mcuId,recordData,timeout,TimeUnit.SECONDS);
        } catch (Exception e) {
        }
    }

    enum RedisType{
        USERINFO,
        TOKEN_SECRET,
        CLIENTID,
        YHUBDATA,
        UPDATESTATE,
        NOTICE,MCUID,
        SOC,
        LastPushTime,
        LastRTR,
        LastBoot,
        MostRecentRTR,
        WaterLevel,
        ShuntTemp,
        BatteryTemp,
        RTRCache,
        DODCache,
    }


//    private RuntimeSchema<YhubDataModel> schema = RuntimeSchema.createFrom(YhubDataModel.class);
//
//
//    public String putYhubData(String key ,YhubDataModel yhubData) {
//        try {
//            byte[] bytes = ProtostuffIOUtil.toByteArray(yhubData, schema,
//                    LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//            // 超时缓存
//            long timeout = 60 * 60 * 24l * 2;// 24 小时
//            put(key,bytes,timeout);
//            return "bytes";
//
//        } catch (Exception e) {
//
//        }
//
//        return null;
//    }
//
//    public YhubDataModel getYhubData(String key){
//        try {
//            // 并没有实现哪部序列化操作
//            // 采用自定义序列化
//            // protostuff: pojo.
//            String date = get(key);
//            byte[] bytes = date.getBytes();
//            // 缓存重获取到
//            if (bytes != null) {
//                YhubDataModel yhubData = schema.newMessage();
//                ProtostuffIOUtil.mergeFrom(bytes, yhubData, schema);
//                return yhubData;
//            }
//
//        } catch (Exception e) {
//
//        }
//        return null;
//
//    }

    public void put(String key,Object value){
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }

    public void put(String key,Object value, Long expire){
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, expire, TimeUnit.SECONDS);
    }

    public String get(String key){
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        Long expire = valueOperations.getOperations().getExpire(key);
        return valueOperations.get(key);
    }

    public <T> T get(String key,Class<T> result){
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String resultModel = valueOperations.get(key);
        if (null == resultModel || "".equals(resultModel)){
            return null;
        }
        return null;
//        return JSONUtil.stringToObject(resultModel,result);
    }

    public String get(String key, Long expire){
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        Long remainExpire = valueOperations.getOperations().getExpire(key);
        log.info("token=>"+key+"&剩余时间=>"+remainExpire);

        if (remainExpire > 0){
            log.info(key+"【Data】"+remainExpire+"秒后过期，刷新expire时间");
            valueOperations.getOperations().expire(key, expire, TimeUnit.SECONDS);
        }

        return valueOperations.get(key);
    }

    public Set<String> getKeys(String key) {
        return redisTemplate.keys(key);
    }

    public <T> T get(String key,Class<T> result, Long expire){
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        Long remainExpire = valueOperations.getOperations().getExpire(key, TimeUnit.SECONDS);
        log.info("token=>"+key+"&剩余时间=>"+remainExpire);

        if (remainExpire > 0){
            log.info(key+"【Data】"+remainExpire+"秒后过期，刷新expire时间");
            valueOperations.getOperations().expire(key, expire, TimeUnit.SECONDS);
        }
        String resultModel = valueOperations.get(key);
        if (null == resultModel || "".equals(resultModel)){
            return null;
        }
        return null;
//        return JSONUtil.stringToObject(resultModel,result);
    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }

}
